package com.yunji.system.context;


import com.google.common.base.Optional;
import com.yunji.common.enums.common.UserType;
import com.yunji.common.model.dto.LoginInfo;
import com.yunji.common.model.enums.LoginTypeEnum;
import com.yunji.system.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author : peter-zhu
 * @date : 2024/10/22 14:00
 * @description : TODO
 **/
@Component
public class SystemContext {

    private static TokenService tokenService;

    @Autowired
    protected void setTokenService(TokenService tokenService) {
        SystemContext.tokenService = tokenService;
    }

    public static Long getUserId() {
        return Optional.fromNullable(getUser()).transform(LoginInfo::getUserId).orNull();
    }

    public static String getUsername() {
        return Optional.fromNullable(tokenService.getUser()).transform(LoginInfo::getUsername).orNull();
    }

    public static LoginInfo getUser() {
        return tokenService.getUser();
    }

    public static Long getBelongId() {
        return Optional.fromNullable(getUser()).transform(LoginInfo::getBelongId).orNull();
    }

    public static String getBelongName() {
        return Optional.fromNullable(getUser()).transform(LoginInfo::getBelongName).orNull();
    }

    public static LoginTypeEnum getLoginType() {
        return Optional.fromNullable(getUser()).transform(LoginInfo::getLoginType).orNull();
    }

    public static boolean isApp() {
        LoginTypeEnum loginType = getLoginType();
        if (getLoginType() == null)
            return false;
        return loginType.name().equals(LoginTypeEnum.CUSTOMER.name()) || loginType.name().equals(LoginTypeEnum.STAFF.name());
    }

    public static boolean isPlate() {
        return Objects.equals(UserType.PLATE.getCode(), Optional.fromNullable(getUser()).transform(LoginInfo::getUserType).orNull());
    }

    public static boolean isSite() {
        return Objects.equals(UserType.SITE.getCode(), Optional.fromNullable(getUser()).transform(LoginInfo::getUserType).orNull());
    }

    public static boolean isAgent() {
        return Objects.equals(UserType.AGENT.getCode(), Optional.fromNullable(getUser()).transform(LoginInfo::getUserType).orNull());
    }
}
